# DFI provides an OPEN API
require 'date'

module Shin
  module Data
    class Previewnetworks

      def new
        self
      end

      # Fix these before running
      def before(params={})
        raise MissingArgument, "You are missing the argument 'previewnetworks_key' which is required to use this source." unless Shin.get[:previewnetworks_key] != nil

        # Timestamp
        Shin.get[:previewnetworks_country] ||= "se"

        "http://cinema.maxplatform.com/api/json/media-feed/" + Shin.get[:previewnetworks_country]
      end

      # Search (on imdb)
      def search_imdb(params={})
        url = before()
        raise MissingArgument, "You are missing the argument 'imdb_id' which is required to use this source." unless params[:imdb_id] != ""

        # Response
        response = Base.get(url + '/cinema/all-1/' + Shin.get[:previewnetworks_key] + '/?search_field=imdb_id&search_query=' + params[:imdb_id])
        raise HTTPError, "The response didn't have a 200 HTTP Code. It had #{response.code}." unless response.code == 200

        data = Oj.load(response.body) rescue nil

        data.to_hashugar
      end

    end
  end
end
